#include "stdafx.h"

game_machine :: game_machine()
{
    //character_s ch;
    string names[4] = {"Dande", "Nonffe", "Barnaby", "Penus"};

    Vector2f vecs[4] =
    {
        Vector2f(0,10),
        Vector2f(5,10),
        Vector2f(5,5),
        Vector2f(5,0)
    };
    FOR(4)
    {
    	Characters.push_back(character_s());
        CharNames.push_back(names[i]);
        CharPositions.push_back(vecs[i]);
    }
    click_position = Vector2f(5,5);
//	FOR(4)
//	{
//		bars.push_back(Bar());
//		bars[i].setPosition(200,i * 15.0f+10);
//	}
//    avl_bars.push(0);
//    avl_bars.push(1);
//    avl_bars.push(2);
//    avl_bars.push(3);
//    
	load_abilities();

}

void game_machine :: load_abilities()
{
	// getting spell name list
	//Ogre::ConfigFile cf;
	//cf. CROSSLOAD("conf/abils.cfg");

    ynode doc;
    ifstream ifstr("abils.yml");
    YAML::Parser parser(ifstr); // remember to pass it by reference
	if(! parser.GetNextDocument(doc))
        exit(0xbadf00d);

    ability base;
    for(YAML::Iterator a = doc.begin(); a != doc.end(); ++ a)
    {
        string name(get_scalar((*a).begin().first()));
        string parsee = get_scalar((*a).begin().second());
	    //Ogre::StringVector strs = StringUtil::split(parsee);
	    //for(StringVector::iterator it = strs.begin(); it != strs.end(); ++it)
	    //{
        cout << parsee << endl;
		istringstream iss(parsee);
		if(iss)
		{
            memset(&base, 0, sizeof(base));
			string misl, cone, bell, chain;
            short ticks, radius, velocity, range;

            // unwrapping as usual, works better than split: split cannot deal with multi-spaces eg "  ".
            //misl cone bell chain delay ticks rad veloc
			iss >> misl >> cone >> bell >> chain
                >> base.cooltime
                >> ticks >> radius >> velocity >> range;
            base.ticks    = ticks    ;
            base.range    = range    ;
            base.radius   = radius   ;
            base.velocity = velocity ;
            if(misl  == "ye") set_mask8(base.mask, MISSILE); else set_mask8(base.mask, MISSILE, false);
            if(cone  == "ye") set_mask8(base.mask, CONIC  ); else set_mask8(base.mask, CONIC  , false);
            if(bell  == "ye") set_mask8(base.mask, BELL   ); else set_mask8(base.mask, BELL   , false);
            if(chain == "ye") set_mask8(base.mask, CHAIN  ); else set_mask8(base.mask, CHAIN  , false);

            if (base.cooltime == 0.f)
                base.cooltime = -1.f; 
            
		    Abilities.push_back(base);

            AbilIds[name] = Abilities.size() - 1;
		    AbilNames.push_back(name);
		}
	}
    
    for(vector<ability>::iterator a = Abilities.begin(); a!=Abilities.end(); ++a)
    {
        ostringstream iss;
        //cout 
        iss
        << AbilNames[std::distance(Abilities.begin(), a)] << "\t"
        << a->cooltime << " "
        << short(a->radius) << " "
        << short(a->ticks) << " "
        << short(a->range) << " "
        << short(a->velocity) << " "
        << inspect_mask(a->mask) <<
        endl;
        LOGMSG(iss.str());
        
    }
    ostringstream oss;
    //cout
    oss
    //<< "Abilities AbilNames CharNames Characters CharPositions Timers avail" << endl 
    << "Abilities " << Abilities.size() << " "
    << "AbilNames " << AbilNames.size() << " "
    << "CharNames " << CharNames.size() << " "
    << "Characters " << Characters.size() << " "
    << "CharPositions " << CharPositions.size() << " "

    << "Timers " << Timers.size() << " "
    //<< "availables " << availables.size() << " "
    
    << endl;
    LOGMSG(oss.str());
}
